<template>
  <!-- 放置一个图标 -->
  <div>
    <!-- svg-icon内置全局组件：
    用法：icon-class="svg目录下图片名字"

    全屏图标切换：
    1.不是全屏：fullscreen
    2.全屏：exit-fullscreen
     -->
    <svg-icon
      :icon-class="isFull ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
import sf from "screenfull";
export default {
  name: "ScreenFull",
  data() {
    return {
      // 是否是全屏：false 不是 | true 是
      isFull: false,
    };
  },
  created() {
    this.watchScreen();
  },
  methods: {
    // 点击切换全屏
    toggleScreen() {
      // 浏览器兼容性判断：是否支持全屏：sf.isEnabled = true
      if (sf.isEnabled) {
        // 实现全屏
        sf.toggle();
      }
    },
    // 监听全屏状态
    watchScreen() {
      if (sf.isEnabled) {
        sf.on("change", () => {
          // console.log('全屏状态：',sf.isFullscreen)
          this.isFull = sf.isFullscreen;
        });
      }
    },
  },
  // 组件销毁解绑监听事件
  destroyed () {
    if(sf.isEnabled) {
        sf.off('change')
    }
  }
};
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>